package com.ssm.zyn.action;

import com.ssm.zyn.eo.StudentEOpage;
import com.ssm.zyn.model.Student;
import com.ssm.zyn.service.Studentservice;
import com.ssm.zyn.tool.ResponseMessage;
import com.ssm.zyn.tool.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "student")
@RestController
@CrossOrigin
public class StudentController {
    @Autowired
    private Studentservice studentservice;

    @ApiOperation(value = "学生添加方法")
    @RequestMapping(value = "/addStudent", method = RequestMethod.GET)
    public ResponseMessage AddStudent(Student student) {
        int IsAdd = 0;
        //传入数据的判断
        //（1）实体对象不能为空
        if (student != null) {
            //(2)学号不能为空
            if (student.getStuid() != null) {
                //（3）添加角色数据
                IsAdd = studentservice.AddtStudent(student);
                if (IsAdd == 1) {
                    return Result.success("保存成功");
                } else if (IsAdd == -1) {
                    return Result.error("学号重复不允许添加");
                } else {
                    return Result.error("保存失败");
                }
            }
        }
        return Result.error("添加失败");
    }

    @ApiOperation(value = "学生修改方法")
    @RequestMapping(value = "/updateStudent", method = RequestMethod.GET)
    public ResponseMessage updateStudent(Student student) {
        int isUpdate = 0;
        //传入的参数不能为空
        if (student != null) {
            //参数当中主键不能为0
            if (student.getSid() != 0) {
                isUpdate = studentservice.UpdateStudent(student);
                if (isUpdate == 1) {
                    return Result.success("更新成功");
                } else {
                    return Result.error("学号重复，更新失败");
                }

            }
        }
        return Result.error("传入参数不正确");
    }

    @ApiOperation(value = "学生删除方法")
    @RequestMapping(value = "/delStudent", method = RequestMethod.GET)
    public ResponseMessage delStudent(int sid) {
        if (sid!=0) {
            int delInt = 0;
            delInt = studentservice.DeleteStudent(sid);
            if (delInt > 0) {
                return Result.success("删除成功");
            }
        }else {
            return Result.error("删除失败");
        }
        return Result.error("删除失败");
    }

    @ApiOperation(value = "根据条件获取学生信息")
    @RequestMapping(value = "/getStudentAll",produces = "application/json;charset=utf-8",method = RequestMethod.POST)
    public ResponseMessage getStudentAll(@RequestBody StudentEOpage studentEOpage){
        Map<String, List> map=new HashMap<>();
        List<StudentEOpage> listStudent=studentservice.getWhereAll(studentEOpage);
        map.put("list",listStudent);
        return Result.success("200",map);
    }
}
